package com.hansheng.action.tailing;

import com.hansheng.service.impl.SheetServiceImpl;
import com.hansheng.util.JsonHelper;
import com.hansheng.util.PrintWriterHelper;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.hansheng.util.JsonHelper.OPER_FAILED;

/**
 * Created by GoldenMan on 2017/5/12.
 */
public class SaveTailingAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
    private SheetServiceImpl sheetService;
    private HttpServletResponse httpServletResponse;
    private HttpServletRequest httpServletRequest;
    private String tailingDetails;
    private int outId;
    private int operatorId;
    public void setSheetService(SheetServiceImpl sheetService) {
        this.sheetService = sheetService;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.httpServletRequest = request;
    }

    public String getTailingDetails() {
        return tailingDetails;
    }

    public void setTailingDetails(String tailingDetails) {
        this.tailingDetails = tailingDetails;
    }

    public int getOutId() {
        return outId;
    }

    public void setOutId(int outId) {
        this.outId = outId;
    }

    public int getOperatorId() {
        return operatorId;
    }

    public void setOperatorId(int operatorId) {
        this.operatorId = operatorId;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.httpServletResponse = response;
        httpServletResponse.setContentType("text/html;charset=utf-8");
        httpServletResponse.setHeader("Cache-Control", "no-cache");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
    }

    @Override
    public String execute() throws Exception {
        boolean flag=sheetService.saveTailings(getOutId(),getOperatorId(),tailingDetails);
        PrintWriterHelper.send(flag? JsonHelper.successJSONBuilder():JsonHelper.errJSONBulider(1,OPER_FAILED));
        return null;
    }
}
